package com.shyfu.entities;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.sql.Timestamp;
import java.util.List;

/**
 * Created by Svitlana on 03-Apr-14.
 */
@Entity
@Table(name = "USERS", schema = "SHYFU", catalog = "")
public class User {
    private long userId;
    @NotNull
    @NotEmpty
    @Size(max = 100)
    private String firstName;
    @NotNull
    @NotEmpty
    @Size(max = 100)
    private String lastName;
    @NotNull
    @NotEmpty
    @Size(max = 100)
    private String username;
    @NotNull
    @NotEmpty
    @Size(max = 100)
    private String password;
    @NotNull
    @NotEmpty
    @Size(max = 100)
    private String email;
    @Size(max = 100)
    private String img;
    @Size(max = 4000)
    private String additional;
    @NotNull
    private int enabled;
    private Timestamp birthDate;
    private List<Comment> comments;
    private List<Book> books;

    @Id
    @Column(name = "USER_ID", nullable = false, insertable = true, updatable = true, precision = 0)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_seq")
    @SequenceGenerator(name = "user_seq", sequenceName = "USERIDSEQUENCE", allocationSize = 1)
    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    @Basic(fetch = FetchType.EAGER)
    @Column(name = "FNAME", nullable = false, insertable = true, updatable = true, length = 100)
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String fname) {
        this.firstName = fname;
    }

    @Basic(fetch = FetchType.EAGER)
    @Column(name = "LNAME", nullable = false, insertable = true, updatable = true, length = 100)
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lname) {
        this.lastName = lname;
    }

    @Basic(fetch = FetchType.EAGER)
    @Column(name = "USERNAME", nullable = false, insertable = true, updatable = true, length = 100)
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Basic(fetch = FetchType.EAGER)
    @Column(name = "PASSWORD", nullable = false, insertable = true, updatable = true, length = 100)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Basic(fetch = FetchType.EAGER)
    @Column(name = "EMAIL", nullable = false, insertable = true, updatable = true, length = 100)
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Basic(fetch = FetchType.EAGER)
    @Column(name = "IMG", nullable = true, insertable = true, updatable = true, length = 100)
    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    @Basic(fetch = FetchType.EAGER)
    @Column(name = "ADDITIONAL", nullable = true, insertable = true, updatable = true, length = 4000)
    public String getAdditional() {
        return additional;
    }

    public void setAdditional(String additional) {
        this.additional = additional;
    }

    @Basic(fetch = FetchType.EAGER)
    @Column(name = "BDATE", nullable = true, insertable = true, updatable = true)
    public Timestamp getBirthDate() {
        return birthDate;
    }

    public void setBirthDate(Timestamp bdate) {
        this.birthDate = bdate;
    }

    @OneToMany
    @JoinColumn(name = "COMM_ID")
    @JsonIgnore
    public List<Comment> getComments() {
        return comments;
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
    }

    @OneToMany
    @JoinColumn(name = "USER_ID")
    @JsonIgnore
    public List<Book> getBooks() {
        return books;
    }

    public void setBooks(List<Book> books) {
        this.books = books;
    }

    @Basic(fetch = FetchType.EAGER)
    @Column(name = "ENABLED")
    public int getEnabled() {
        return enabled;
    }

    public void setEnabled(int enabled) {
        this.enabled = enabled;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;

        if (userId != user.userId) return false;
        if (additional != null ? !additional.equals(user.additional) : user.additional != null) return false;
        if (birthDate != null ? !birthDate.equals(user.birthDate) : user.birthDate != null) return false;
        if (email != null ? !email.equals(user.email) : user.email != null) return false;
        if (firstName != null ? !firstName.equals(user.firstName) : user.firstName != null) return false;
        if (img != null ? !img.equals(user.img) : user.img != null) return false;
        if (lastName != null ? !lastName.equals(user.lastName) : user.lastName != null) return false;
        if (password != null ? !password.equals(user.password) : user.password != null) return false;
        if (username != null ? !username.equals(user.username) : user.username != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        long result = userId;
        result = 31 * result + (firstName != null ? firstName.hashCode() : 0);
        result = 31 * result + (lastName != null ? lastName.hashCode() : 0);
        result = 31 * result + (username != null ? username.hashCode() : 0);
        result = 31 * result + (password != null ? password.hashCode() : 0);
        result = 31 * result + (email != null ? email.hashCode() : 0);
        result = 31 * result + (img != null ? img.hashCode() : 0);
        result = 31 * result + (additional != null ? additional.hashCode() : 0);
        result = 31 * result + (birthDate != null ? birthDate.hashCode() : 0);
        return ((Long) result).intValue();
    }
}